﻿/*********
* Copyright © 深圳润衡信息技术有限公司 . All rights reserved.
* 
*  Description:
*  Create By $username at $time
*  
*
*  Revision History:
*  Date                  Who                 What
*  
* 
*/

#region namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

#endregion

namespace Janus.Cores.Globalization
{
    /// <summary>
    /// 多语言项
    /// </summary>
    [Serializable]
    [DataContract]
    public partial class Entry
    {
        List<Item> _lst = new List<Item>();
        string _zhCN;
        /// <summary>
        /// 
        /// </summary>
        public Entry()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string Key { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string Flag { get; set; }

        /// <summary>
        /// 中文
        /// </summary>
        public string zhCN
        {
            get
            {
                if (string.IsNullOrEmpty(_zhCN))
                {
                    _zhCN = this["zh-CN"];
                }
                return _zhCN;
            }
        }

        /// <summary>
        /// 获取Key对应的当前语言环境的翻译值
        /// </summary>
        /// <param name="lng">多语言key</param>
        /// <returns>翻译结果</returns>
        public string this[string lng]
        {
            get
            {
                var item = _lst.FirstOrDefault(c => c.Key == lng);

                if (item == null)
                    return string.Empty;
                else
                    return item.Value;
            }
            set
            {
                if (string.Compare("zh-CN", lng, true) == 0)
                    _zhCN = value;
                var item = _lst.FirstOrDefault(c => c.Key == lng);
                if (item == null)
                {
                    item = new Item(lng, value);
                    _lst.Add(item);
                }
                else
                {
                    item.Value = value;
                }

            }
        }

        [DataMember]
        internal List<Item> Items
        {
            get { return _lst; }
            set { _lst = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            var e = obj as Entry;
            if (e == null)
                return false;
            return string.Compare(e.Key, Key, true) == 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Key.GetHashCode();
        }
    }


    [Serializable]
    [DataContract]
    class Item
    {
        public Item() { }

        public Item(string key, string value)
        {
            Key = key;
            this.Value = value;
        }

        [DataMember]
        public string Key { get; set; }

        [DataMember]
        public string Value { get; set; }
    }
}
